﻿using Basic.WxApiRemoteModel;
using Basic.WxApiRemoteModel.WxMsg;
using Basic.WxModel.WxMsgFilter;
using Basic.WxRemoteModel.WxMsg.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Client.Interface;
using WeDonekRpc.Model;

namespace Basic.WxService
{
    internal static class LinqHelper
    {
        public static string IsNull (this string str, string def)
        {
            return string.IsNullOrEmpty(str) ? def : str;
        }
        public static void Send (this WxMsgFilterDto msg, IRemoteSendService service, WxMsgBody body)
        {
            WxBasicMsg data = body.ConvertMap<WxMsgBody, WxBasicMsg>();
            if (msg.ForwardType == WxForwardType.指令)
            {
                _SendDirect(msg, service, data);
            }
            else
            {
                _SendBroadcast(msg, service, data);
            }
        }
        public static void Send (this WxMsgFilterDto msg, IRemoteSendService service, WxMsgEventBody body)
        {
            WxEventMsg data = body.ConvertMap<WxMsgEventBody, WxEventMsg>();
            if (msg.ForwardType == WxForwardType.指令)
            {
                _SendDirect(msg, service, data);
            }
            else
            {
                _SendBroadcast(msg, service, data);
            }
        }
        private static void _SendBroadcast<T> (WxMsgFilterDto msg, IRemoteSendService service, T body) where T : class
        {
            IRemoteBroadcast broadcast = new IRemoteBroadcast(msg.Direct, msg.RpcMerId)
            {
                IsExclude = true,
                TypeVal = new string[]
                {
                    msg.SystemType
                }
            };
            if (msg.Reply != null)
            {
                broadcast.RegionId = msg.Reply.RegionId;
                broadcast.BroadcastType = msg.Reply.BroadcastType.GetValueOrDefault(BroadcastType.默认);
                broadcast.IsOnly = msg.Reply.IsOnly.GetValueOrDefault(true);
                RemoteSet config = new RemoteSet
                {
                    IsAllowRetry = msg.Reply.IsAllowRetry.GetValueOrDefault(true),
                    IsEnableLock = msg.Reply.IsEnableLock.GetValueOrDefault(false),
                    TransmitType = msg.Reply.TransmitType.GetValueOrDefault(TransmitType.close),
                    TimeOut = msg.Reply.TimeOut,
                    RetryNum = msg.Reply.RetryNum
                };
                if (config.IsEnableLock)
                {
                    config.LockType = RemoteLockType.普通锁;
                    config.LockColumn = new string[]
                    {
                            "MsgId"
                    };
                }
                if (config.TransmitType != TransmitType.close)
                {
                    config.Transmit = msg.Reply.Transmit;
                    config.IdentityColumn = msg.Reply.TransmitKey;
                }
                broadcast.RemoteConfig = config;
            }
            service.BroadcastMsg<T>(broadcast, body);
        }
        private static void _SendDirect<T> (WxMsgFilterDto msg, IRemoteSendService service, T body) where T : class
        {
            IRemoteConfig config = new IRemoteConfig(msg.Direct, msg.SystemType, false);

            if (msg.Reply != null)
            {
                config.IsAllowRetry = msg.Reply.IsAllowRetry.GetValueOrDefault(true);
                config.IsEnableLock = msg.Reply.IsEnableLock.GetValueOrDefault(false);
                config.TransmitType = msg.Reply.TransmitType.GetValueOrDefault(TransmitType.close);
                config.TimeOut = msg.Reply.TimeOut;
                config.RetryNum = msg.Reply.RetryNum;
                config.RegionId = msg.Reply.RegionId;
                config.RpcMerId = msg.RpcMerId;
                if (config.IsEnableLock)
                {
                    config.LockType = RemoteLockType.普通锁;
                    config.LockColumn = new string[]
                    {
                            "MsgId"
                    };
                }
                if (config.TransmitType != TransmitType.close)
                {
                    config.Transmit = msg.Reply.Transmit;
                    config.IdentityColumn = msg.Reply.TransmitKey;
                }
            }
            service.Send<T>(config, body);
        }
    }
}
